package game.module.mj.logic;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.module.mj.bean.MjRoom;

public class MjDataManager {
	
    private static Logger logger = LoggerFactory.getLogger(MjDataManager.class);
    
    static class SingletonHolder{
    	static MjDataManager instance = new MjDataManager();
    }
    
    public static MjDataManager getInstance(){
    	return SingletonHolder.instance;
    }
    
    private Map<String,MjRoom> roomMap = new ConcurrentHashMap<>();
    
    private Map<Integer,String> playerRoomMap = new ConcurrentHashMap<>();
    
    public boolean isPlayerInRoom(int playId){
    	return playerRoomMap.containsKey(playId);
    }
    
    public void removeRoom(String roomUuid){
    	MjRoom mjRoom = roomMap.get(roomUuid);
    	if(mjRoom==null){
    		return;
    	}
    	List<Integer> playerList = mjRoom.getPlayerList();
    	for(Integer aPlayerId:playerList){
    		playerRoomMap.remove(aPlayerId);
    	}
    	playerList.clear();
    	roomMap.remove(roomUuid);
    }
    
    public boolean hasRoomUuid(String roomUuid){
    	return roomMap.containsKey(roomUuid);
    }
    
    public void addRoom(MjRoom mjRoom){
    	roomMap.put(mjRoom.getUuid(), mjRoom);
    	playerRoomMap.put(mjRoom.getPlayerList().get(0), mjRoom.getUuid());
    }
    
    public void addPlayer(int playerId,String roomId) {
		playerRoomMap.put(playerId, roomId);
	}
    public boolean checkRoomExist(String room_id){
    	return roomMap.containsKey(room_id);
    }
    public MjRoom getMjRoom(String room_id){
    	return roomMap.get(room_id);
    }
    public MjRoom getMjRoomByPlayerId(int playerId){
    	String roomId = playerRoomMap.get(playerId);
    	if(roomId == null){
    		return null;
    	}
		return roomMap.get(roomId);
    }

    public int getOnlineSize(){
		int ret = 0;
		for(MjRoom aRoom : roomMap.values()){
			ret += aRoom.getPlayerList().size();
		}
		return ret;
	}

}
